home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DDJMAG / DDJ9310.ZIP / DFPP03.ZIP / TED.CPP < prev    next >
C/C++ Source or Header  |  1993-09-27  |  4KB  |  173 lines

  1. // ------------- ted.cpp
  2.  
  3. #include <fstream.h>
  4. #include "ted.h"
  5.  
  6. static char untitled[] = "(untitled)";
  7.  
  8. main(int argc, char *argv[])
  9. {
  10.     Ted ma;
  11.     if (argc > 1)
  12.         ma.OpenFile(argv[1]);
  13.     ma.Execute();
  14.     return 0;
  15. }
  16.  
  17. // ------- editor colors
  18. static Color col = {
  19.     BLACK,                // fg
  20.     LIGHTGRAY,            // bg
  21.     LIGHTGRAY,            // selected fg
  22.     BLACK,                // selected bg
  23.     LIGHTGRAY,            // frame bg
  24.     BLUE,                // frame fg
  25.     LIGHTGRAY,            // highlighted fg
  26.     BLUE                // highlighted bg
  27. };
  28.  
  29. // ---- construct application
  30. Ted::Ted() :  menubar(TedMenu, this),
  31.               toolbar(this),
  32.               editor(ClientLeft(),
  33.                      ClientTop(),
  34.                      ClientHeight(),
  35.                      ClientWidth(),
  36.                      this),
  37.               fname(untitled)
  38.                  
  39. {
  40.     SetAttribute(SIZEABLE | MOVEABLE);
  41.     editor.SetAttribute(VSCROLLBAR | BORDER);
  42.     editor.SetColor(col);
  43.     SetClearChar(' ');
  44.     BuildTitle();
  45.     Show();
  46.     editor.SetFocus();
  47. }
  48.  
  49. // ---- builds the title with the current document name
  50. void Ted::BuildTitle()
  51. {
  52.     SetTitle(String("TED: ") + fname);
  53.     Title();
  54. }
  55.  
  56. // ---- File/New Menu Command
  57. void Ted::CmNew()
  58. {
  59.     TestChanged();
  60.     editor.ClearText();
  61.     fname = String(untitled);
  62.     BuildTitle();
  63.     editor.Paint();
  64. }
  65.  
  66. // ---- Open and load a specified file
  67. void Ted::OpenFile(String fn)
  68. {
  69.     editor.ClearText();
  70.     fname = fn;
  71.     BuildTitle();
  72.     editor.ClearChanged();
  73.  
  74.     ifstream tfile(fname);
  75.     if (tfile)    {
  76.         // --- first get the file size
  77.         tfile.seekg(0, ios::end);
  78.         long sz = tfile.tellg();
  79.         tfile.seekg(0, ios::beg);
  80.         // --- buffer to read file into
  81.         char *ip = new char[sz+1];
  82.         memset(ip, 0, sz+1);
  83.         // --- read the file
  84.         tfile.read(ip, sz);
  85.         // --- put the text into the editor window
  86.         editor.SetText(ip);
  87.         // --- not needed any more
  88.         delete [] ip;
  89.         editor.Paint();
  90.     }
  91. }
  92.  
  93. // ---- File/Open Menu Command
  94. void Ted::CmOpen()
  95. {
  96.     TestChanged();
  97.     FileOpen fo("*.txt");
  98.     fo.Execute();
  99.     if (fo.OKExit())
  100.         OpenFile(fo.FileName());
  101. }
  102.  
  103. // ---- File/Save Menu Command
  104. void Ted::CmSave()
  105. {
  106.     if (fname == String(untitled))
  107.         CmSaveAs();
  108.     if (fname != String(untitled))    {
  109.         editor.ClearChanged();
  110.         ofstream tfile(fname);
  111.         tfile.write(editor.GetText(), editor.TextLength());
  112.     }
  113. }
  114.  
  115. // ---- File/Save As Menu Command
  116. void Ted::CmSaveAs()
  117. {
  118.     SaveAs sa;
  119.     sa.Execute();
  120.     if (sa.OKExit())    {
  121.         fname = String(sa.FileName());
  122.         BuildTitle();
  123.         CmSave();
  124.     }
  125. }
  126.  
  127. // ---- Edit/Paragraph Menu Command
  128. void Ted::CmPara()
  129. {
  130.     editor.FormParagraph();
  131. }
  132.  
  133. // ---- Options/Insert Menu Command
  134. void Ted::CmInsert()
  135. {
  136.     editor.SetInsertMode(InsertCmd.isToggled());
  137. }
  138.  
  139. // ----- reset the editor focus when the application moves
  140. void Ted::Move(int x, int y)
  141. {
  142.     Application::Move(x, y);
  143.     editor.SetFocus();
  144. }
  145.  
  146. // ----- resize the editor when the application resizes
  147. void Ted::Size(int x, int y)
  148. {
  149.     editor.Hide();
  150.     editor.Size(editor.Right()+(x-Right()),
  151.                 editor.Bottom()+(y-Bottom()));
  152.     Application::Size(x, y);
  153.     editor.SetFocus();
  154. }
  155.  
  156. // ---- test for changes to the document before discarding
  157. void Ted::TestChanged()
  158. {
  159.     if (editor.Changed())    {
  160.         String msg(fname + " has changed. Save?");
  161.         if (YesNo(msg))
  162.             CmSave();
  163.     }
  164. }
  165.  
  166. // ---- test for changes before closing
  167. void Ted::CloseWindow()
  168. {
  169.     TestChanged();
  170.     Application::CloseWindow();
  171. }
  172.  
  173.